package tech.zjp.ssm.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tech.zjp.ssm.entity.User;
import tech.zjp.ssm.mapper.UserMapper;
import tech.zjp.ssm.service.UserService;

/**
 *
 * @Description: 
 * @Author: 周军鹏
 * @Date: 2020年3月4日 下午2:33:52
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User login(String userCode, String userPassword) {
		User user = userMapper.getLoginUser(userCode);
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		return user;
	}

	@Override
	public boolean add(User user) {
		return userMapper.add(user) > 0?true:false;
	}

	@Override
	public List<User> getUserList(String queryUserName, Integer queryUserRole, Integer pageNum,
			Integer pageSize) {
		return userMapper.getUserList(queryUserName, queryUserRole, (pageNum-1)*pageSize, pageSize);
	}

	@Override
	public int getUserCount(String queryUserName, Integer queryUserRole) {
		return userMapper.getUserCount(queryUserName, queryUserRole);
	}

	@Override
	public User selectUserCodeExist(String userCode) {
		return userMapper.getLoginUser(userCode);
	}

	@Override
	public boolean deleteUserById(Integer delId) {
		File upload = new File("D://upload//");
		boolean flag = true;
		//先删除该条记录的上传附件
		User user = userMapper.getUserById(delId);
		if(user.getIdPicPath() != null && !user.getIdPicPath().equals("")){
			//删除服务器上个人证件照
			File file = new File(upload, user.getIdPicPath());
			if(file.exists()){
				flag = file.delete();
			}
		}
		if(flag && user.getWorkPicPath() != null && !user.getWorkPicPath().equals("")){
			//删除服务器上个人工作证照片
			File file = new File(upload, user.getWorkPicPath());
			if(file.exists()){
				flag = file.delete();
			}
		}
		if(flag){
			if(userMapper.deleteUserById(delId) < 1) {
				flag = false;
			}
		}
		return flag;
	}

	@Override
	public User getUserById(Integer id) {
		return userMapper.getUserById(id);
	}

	@Override
	public boolean modify(User user) {
		return userMapper.modify(user) > 0?true:false;
	}

	@Override
	public boolean updatePwd(Integer id, String pwd) {
		return userMapper.updatePwd(id, pwd) > 0?true:false;
	}

}
